package com.bawei.module_inspection.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.module_inspection.R
import com.bawei.module_inspection.model.PatrolModelItem

class InspectionAdapter : RecyclerView.Adapter<InspectionAdapter.InspectionViewHolder>() {


    private val diffCallback = object : DiffUtil.ItemCallback<PatrolModelItem>() {
        override fun areItemsTheSame(oldItem: PatrolModelItem, newItem: PatrolModelItem): Boolean {
            return oldItem.id == newItem.id // 根据唯一标识判断 item 是否相同
        }

        override fun areContentsTheSame(oldItem: PatrolModelItem, newItem: PatrolModelItem): Boolean {
            return oldItem == newItem // 判断 item 内容是否相同，数据类重写了 equals 可直接用
        }
    }

    fun submitList(newData : List<PatrolModelItem>) = differ.submitList(newData)

    private val differ = AsyncListDiffer(this,diffCallback)

    // ViewHolder：持有列表项的控件引用
    class InspectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        val tvTypeTag: TextView = itemView.findViewById(R.id.tv_type_tag)
        val tvPointLocation: TextView = itemView.findViewById(R.id.tv_point_location)
        val tvPointStatus: TextView = itemView.findViewById(R.id.tv_point_status)


        fun bind(item: PatrolModelItem) {
            tvTitle.text = item.pointName
            tvTypeTag.text = item.patrolType
            tvPointLocation.text = "点位位置: ${item.pointLocation}"
            tvPointStatus.text = "点位状态: 未启用"

            when(item.patrolType){
                "安防巡检" -> tvTypeTag.setBackgroundResource(R.drawable.orange_bg)
                "保洁巡检" -> tvTypeTag.setBackgroundResource(R.drawable.blue_bg)
                "日常巡检" -> tvTypeTag.setBackgroundResource(R.drawable.pink_bg)
                "房屋巡检" -> tvTypeTag.setBackgroundResource(R.drawable.red_bg)
                else -> tvTypeTag.setBackgroundResource(R.drawable.orange_bg)
            }

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InspectionViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_inspection, parent, false)
        return InspectionViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: InspectionViewHolder, position: Int) {
        val inspectionItem = differ.currentList[position]
        holder.itemView.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.InspectionDetailActivity).withInt("id",inspectionItem.id).withString("pointName",inspectionItem.pointName).withString("pointLocation",inspectionItem.pointLocation).withString("patrolStandard",inspectionItem.patrolStandard).navigation()
        }
        holder.bind(inspectionItem)
    }

    override fun getItemCount(): Int {
        return differ.currentList.size
    }
}